package com.css.fxfzxczh.modules.earthquakeLossRatio.repository.impl;

import com.css.fxfzxczh.modules.earthquakeLossRatio.entity.EarthquakeLossRatio;
import com.css.fxfzxczh.modules.earthquakeLossRatio.repository.EarthquakeLossRatioRepository;
import com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.entity.VehManage;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @Author:
 * @CreateTime: 2024/3/25 16:26
 * @Description:
 */
@Repository
public class EarthquakeLossRatioRepositoryImpl implements EarthquakeLossRatioRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public void save(EarthquakeLossRatio earthquakeLossRatio) {
        String sql="insert into xczh_earthquake_loss_ratio(id,xsem_id,loss_ratio1,loss_ratio2,loss_ratio3," +
                "create_user,create_time,update_user,update_time) " +
                "values(:id, :xsemId, :lossRatio1, :lossRatio2, :lossRatio3, :createUser, :createTime, :updateUser, :updateTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(earthquakeLossRatio);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void update(EarthquakeLossRatio earthquakeLossRatio) {
        String sql="update xczh_earthquake_loss_ratio set loss_ratio1 = :lossRatio1,loss_ratio2 = :lossRatio2," +
                "loss_ratio3 = :lossRatio3 ,update_user = :updateUser,update_time = :updateTime where id = :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(earthquakeLossRatio);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public EarthquakeLossRatio findById(String xsemId) {
        EarthquakeLossRatio earthquakeLossRatio=new EarthquakeLossRatio();
        String sql="select * from xczh_earthquake_loss_ratio where xsem_id = '"+xsemId+"'";
        try {
            earthquakeLossRatio = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(EarthquakeLossRatio.class));
        } catch (Exception e) {
            earthquakeLossRatio = null;
        }
        return earthquakeLossRatio;

    }


}
